﻿using System;
using System.IO;

namespace BaseSyntax.Values.StringTest.Yaml.Utils
{
	public class FileUtils
	{
		public static string ReadUtf8File(string filePath)
		{
			if (File.Exists(filePath))
			{
				return File.ReadAllText(filePath);
			}
			return "";
		}

		// 遍历文件夹以及子文件夹，包含文件
		public static void IteratorDirFiles(
			string sourcePath,
			Action<string, string> onFile,
			Func<string, bool> isIgnoreDirs = null
		)
		{
			if (System.IO.Directory.Exists(sourcePath))
			{
				string[] files = Directory.GetFiles(sourcePath);
				string[] dirs = Directory.GetDirectories(sourcePath);

				string fileName = "";
				
				foreach (string sourceFilePath in files)
				{
					fileName = Path.GetFileName(sourceFilePath);

					onFile(fileName, sourcePath);
				}

				// 递归复制文件夹
				foreach (string dir in dirs)
				{
					string dir1 = dir.Replace(@"\", "/");
					
					if (isIgnoreDirs != null)
					{
						if (isIgnoreDirs(dir1))
						{
							continue;
						}
					}

					IteratorDirFiles(dir1, onFile, isIgnoreDirs);
				}
			}
		}
	}
}